package study02;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

public class Java15_utils_class {
    public static void main(String[] args) {
        String s = "wc";
        String s1 = "";
        String s2 = "  ";
        String s3 = null;
        System.out.println(StringUtils.isEmpty(s));
        System.out.println(StringUtils.isEmpty(s1));
        System.out.println(StringUtils.isEmpty(s2));
        System.out.println(StringUtils.isEmpty(s3));

        System.out.println(StringUtils.makeString());
        System.out.println(StringUtils.makeString("abcdefg123456789", 5));
    }
}

class StringUtils {
//    是否为空
    public static boolean isEmpty(String str) {
//        null  空格  空字符串   都是空
        if (str == null || str == "" || str.replace(" ", "") == "") {
            return true;
        } else {
            return false;
        }
    }
    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

//    随机字符串
    public static String makeString() {
        return UUID.randomUUID().toString();
    }

    public static String makeString(String from, int len) {
        if (len < 1) {
            return "";
        } else {
            char[] chars = from.toCharArray();
            StringBuilder str = new StringBuilder();
            for (int i = 0; i < len; i++) {
                Random random = new Random();
                int j = random.nextInt(chars.length);
                char c = chars[j];
                str.append(c);
            }
            return str.toString();
        }
    }

//    转换字符串    ISO123 => UTF-8
    public static String transform(String soure, String encodeFrom, String encodeTo) throws Exception {
//        soure 原数据   encodeFrom 原编码   encodeTo
        byte[] bytes = soure.getBytes(encodeFrom);
        return new String(bytes, encodeTo);
    }

//    日期和字符串的相互转化
    public static Date parseDate(String dateString, String format) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.parse(dateString);
    }

    public static String formatDate(Date date, String format) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }
}
